﻿using System;
using System.Collections.Generic;
using System.Linq;
using Synthetic.IVS.Plugins.Custom;

namespace Synthetic.IVS.Plugins.Loader
{
    class Program
    {
        private static Dictionary<int, string> Options = new Dictionary<int, string>();
        private static int OptionNumber = 1;
        private static IPluginInterface[] plugins;

        static void Main(string[] args)
        {
                Console.Title = "MicroApps++ Loader by Connor Harries";
                Console.WriteLine("Scanning for MicroApps..");
                plugins = PluginLoader.GetPlugins(AppDomain.CurrentDomain.BaseDirectory);
                Console.WriteLine("{0} MicroApps were found in the scan!", plugins.Count());
                if (plugins.Count() == 0)
                {
                    Environment.Exit(1);
                }
                else if (plugins.Count() == 1)
                {
                    plugins[0].MainFunction();
                }
                else
                {
                    for (int i = 0; i < plugins.Count(); i++)
                    {
                        Options.Add(i, plugins[i].Name);
                    }
                    SelectOption();
                }
        }

        private static void SelectOption()
        {
            foreach (string o in Options.Values)
            {
                Console.WriteLine(OptionNumber + ". " + o.ToString());
                OptionNumber += 1;
            }
            Console.Write("Enter number of MicroApp you wish to load: ");
            int opts;
            string x = Console.ReadLine();
            bool parsed;
            parsed = int.TryParse(x, out opts);
            ExecuteOption(opts);
        }

        [STAThread]
        private static void ExecuteOption(int PluginNumber)
        {
            plugins = PluginLoader.GetPlugins(AppDomain.CurrentDomain.BaseDirectory);
            plugins[PluginNumber - 1].MainFunction();
        }
    }
}
